from flask import Flask
from flask import render_template
from flask import request
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import MigrateCommand, Migrate
from flask_script import Manager

from redis import StrictRedis

from config import DevelopementConfig

app = Flask(__name__)

Config = DevelopementConfig
# 加载配置类到flask项目中
app.config.from_object(Config)

db = SQLAlchemy(app)

from models import *

# def create_app():
#     """初始化flask项目的函数"""
#
#     # flask实例对象的初始化
#     app = Flask(__name__)
#     Config = DevelopementConfig
#     # 加载配置类到flask项目中
#     app.config.from_object(Config)
#     global db
#     db = SQLAlchemy(app)
#     # 加载配置信息到SQLAlchemy操作对象中
#     db.init_app(app)
#
#     # 实例化redis操作对象
#     global redis_store
#     redis_store = StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT, db=Config.REDIS_DATA_DB)
#
#     return app


# app = create_app()

manager = Manager(app)

Migrate(app, db)

manager.add_command('db', MigrateCommand)


# 定义视图函数
@app.route('/', methods=['GET', 'POST'])
def index():
    # 往模板中传入数据
    data = Exam.query.all()

    # exam_info = [
    #     {"questions": "出淤泥而不染，濯清涟而不妖”是周敦颐称赞什么的名句？",
    #      "answers": "C",
    #      "options": ["A.牡丹", "B.菊花", "C.莲花", "D.梅花"]
    #      },
    #     {"questions": "出淤泥而不染，濯清涟而不妖”是周敦颐称赞什么的名句11？",
    #      "answers": "D",
    #      "options": ["A.牡丹1", "B.菊花1", "C.莲花1", "D.梅花1"]
    #      }
    # ]
    exam_info = []
    for i in data:
        dic = {}
        dic["questions"] = i.questions
        dic["answers"] = i.answers
        dic["options"] = i.options.split('\n')

        exam_info.append(dic)
    # 1.获取参数
    # exam_info = request.args
    # questions = exam_info.get()
    # answers = exam_info.get()
    # optinos = exam_info.get()
    # exam_info=exam_info.objects.all()
    if request.method == 'GET':
        return render_template('index.html', exam_info=exam_info)


@app.route('/', methods=['GET', 'POST'])
def result():
    return render_template('result.html')


# 运行项目
if __name__ == '__main__':
    manager.run()
